# 导包
import pymysql

# 创建连接对象
conn = pymysql.connect(host='localhost',port=3306,user='root',password='root',database='advanced',charset='utf8')

# 获取游标对象
cursor = conn.cursor()
try:
    sql = 'insert into student (`stu_no`,`stu_name`,`stu_age`,`class_id`,`money`) values("itsrc-017","燕子",18,1,1000)'
    rows = cursor.execute(sql)
    print(f"受影响的行数为:{rows}")
    # 没有异常,提交事务
    conn.commit()
    # 新增成功后的id
    print(cursor.lastrowid)
except Exception as e:
    print(e)
    # 有异常,回滚事务
    conn.rollback()
finally:
    # 关闭游标对象
    cursor.close()
    # 关闭连接对象
    conn.close()

